LCM and GCD calculator code
#include
using namespace std;
// Function to calculate GCD using Euclidean Algorithm
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Function to calculate LCM
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
cout << "--- GCD & LCM Calculator ---\n";
cout << "Enter two integers: ";
cin >> num1 >> num2;
if (num1 <= 0 || num2 <= 0) {
cout << "Please enter positive integers only.\n";
return 1;
}
int resultGCD = gcd(num1, num2);
int resultLCM = lcm(num1, num2);
cout << "GCD of " << num1 << " and " << num2 << " is: " << resultGCD << endl;
cout << "LCM of " << num1 << " and " << num2 << " is: " << resultLCM << endl;
return 0;
}
Code output
--- GCD & LCM Calculator ---
Enter two integers: 12 18
GCD of 12 and 18 is: 6
LCM of 12 and 18 is: 36